/**
 * Created by yangh on 2016/09/04.
 */
var main = {};
var myScroll;
(function ($) {
    main.init = function () {
    	//阻止滑动
    	/*
        document.addEventListener('touchmove', function (e) {
            e.preventDefault();
        }, false);
		*/
        main.initEvent();
    };

    main.initEvent = function() {
    	/*
        $(".pay-select > div").on("click", function()
        {
            $(".pay-select > div").removeClass("checked");
            $(this).addClass("checked");
        })
        */
        
        $(".nav1 li").on("click",function(){
        	//样式
        	$(".nav1 li").removeClass("active");
            $(this).addClass("active");
            
        	//隐藏域
        	var status = $(this).data("id");
        	$("#status").val(status);
        	
        	//加载数据,替换
        	console.log(status);
        	main.loadData(1);
        })
    };

    /**
     * @func move()
     * @desc 监听滚动条的位置，异步加载列表
     */
    function move() {
        // if (!canLoad) {
        //     return;
        // }

        if (myScroll.y < main.max - 50) {
            // canLoad = false;
        	//加载数据,叠加
        	main.loadData(0);
        	
            myScroll.refresh();
            main.max = myScroll.maxScrollY;
            // canLoad = true;
            console.log(main.max);
        }
    }
    
    
    
    /** 
     * function loadData
     * @param isReplace 1=全局替换 2增量
     */
    main.loadData = function(isReplace){
    	//ajax begin
    	 var status=$("#status").val();
    	 var index = -1;
    	 if($(".details li").length!=0 && isReplace==0){
    		 index=$(".details li:last").data("id");
    	 }
         var html = "";
         $.ajax({
     		type:"post",
     		async:true, 
        	url:path+"/manage/order/more",
        	data:{
        		status:status,
        		index:index
        	},
     		success:function(data){	
     			if('000' == data.code){
     				var html = "";
     				for (var i = 0; i < data.result.length; i++) {
     					var infoUrl = path+"/manage/order/"+data.result[i].id;
     					var payUrl = path+"/pay/"+data.result[i].id;
     					var signUrl = path+"/manage/order/sign/"+data.result[i].id;
     					
     					html +="<li data-id='"+data.result[i].id+"'>";
     					html +="<h4>"+data.result[i].merchant.name+"</h4>";
     					for(var j=0; j<data.result[i].orderItemList.length ;j++){
     						var item = data.result[i].orderItemList[j];
     						html +="<a class=\"goods\" href=\"javascript:;\">";
         					html += "   <div class=\"goodsImg\"><img src='"+item.goods.picurl+"' style=\"height: 80px\"></div>";
         					html +="     <h3>"+item.goods.name+"</h3>";
         					html +="     <p class=\"address\">"+item.goods.title+"</p>";
         					html +="    <p class=\"num1\">￥"+item.goods.price+"元</p>";
         					html +=" </a>";
     					}
     					html +="<div class=\"result\">";
     					html +="	<div class=\"amount\">合计：￥"+data.result[i].total+"元</div>";
     					html +="  <div>";
     					if(data.result[i].paystatus==0){
     						html +="   	<a href='"+payUrl+"' class=\"button pullRight\"  id=\"relation\">付款</a>";
     					}
     					if(data.result[i].deliverstatus==1 && data.result[i].signstatus ==0 ){
     						html +="   	<a href='"+signUrl+"' class=\"button pullRight\"  id=\"relation\">签收</a>";
     					}
     					html +="   	<a href='"+infoUrl+"' class=\"button pullRight\"  id=\"relation\">详情</a>";
     					html +="   </div>";
     					html +="</div>";
     					html +=" </li>";
     				}	
     				if(isReplace==1){
     					$(".details").html(html);
     				}else{
     					$(".details").append(html);
     				}
     				
     				myScroll.refresh();
     	            main.max = myScroll.maxScrollY;
     			}
     		}
     	})
   		//ajax end
    }
    

    /**
     * @func main.scroll()
     * @desc 配置iscroll插件
     */
    main.scroll = function () {
        myScroll = new IScroll("#list", {
            probeType: 3,
            mouseWheel: true,
            click: true,
            preventDefault: false
        });

        main.max = myScroll.maxScrollY;
        console.log(main.max);

        myScroll.on("scroll", move);
    };
})(jQuery);

main.init();

//加载数据,叠加
main.loadData(0);

window.onload = function () {
	main.scroll();
};